Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.6
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'tidyr' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 338)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.02246 13.01924 13.01611 13.01305 13.01005 13.00712 13.00425 13.00143
##   [9] 12.99865 12.99592 12.99322 12.99054 12.98789 12.98525 12.98263 12.98001
##  [17] 12.97739 12.97476 12.97212 12.96946 12.96677 12.96406 12.96131 12.95852
##  [25] 12.95568 12.95279 12.94983 12.94682 12.94373 12.94058 12.93740 12.93419
##  [33] 12.93095 12.92769 12.92441 12.92112 12.91781 12.91450 12.91119 12.90788
##  [41] 12.90458 12.90130 12.89802 12.89477 12.89155 12.88835 12.88518 12.88206
##  [49] 12.87898 12.87594 12.87295 12.87002 12.86715 12.86435 12.86161 12.85894
##  [57] 12.85636 12.85385 12.85143 12.84910 12.84686 12.84472 12.84269 12.84076
##  [65] 12.83883 12.83679 12.83463 12.83237 12.83001 12.82756 12.82502 12.82241
##  [73] 12.81972 12.81696 12.81415 12.81127 12.80835 12.80538 12.80238 12.79934
##  [81] 12.79628 12.79320 12.79011 12.78701 12.78391 12.78081 12.77772 12.77465
##  [89] 12.77161 12.76859 12.76560 12.76266 12.75977 12.75692 12.75414 12.75142
##  [97] 12.74877 12.74620 12.74371 12.74132 12.73901 12.73681 12.73471 12.73273
## [105] 12.73087 12.72913 12.72752 12.72605 12.72473 12.72355 12.72253 12.72167
## [113] 12.72098 12.72046 12.72012 12.71996 12.72000 12.72023 12.72067 12.72132
## [121] 12.72218 12.72326 12.72458 12.72612 12.72791 12.72994 12.73222 12.73476
## [129] 12.73831 12.74350 12.75016 12.75811 12.76719 12.77722 12.78802 12.79943
## [137] 12.81127 12.82337 12.83556 12.84766 12.85950 12.87091 12.88172 12.89175
## [145] 12.90083 12.90879 12.91545 12.92308 12.93377 12.94709 12.96260 12.97989
## [153] 12.99851 13.01804 13.03806 13.05813 13.07781 13.09670 13.11434 13.13032
## [161] 13.14420 13.15556 13.16397 13.17103 13.17863 13.18670 13.19521 13.20410
## [169] 13.21334 13.22286 13.23263 13.24260 13.25272 13.26294 13.27321 13.28349
## [177] 13.29374 13.30389 13.31392 13.32376 13.33337 13.34270 13.35172 13.36036
## [185] 13.36858 13.37634 13.38359 13.39027 13.39635 13.40177 13.40650 13.41047
## [193] 13.41365 13.41598 13.41742 13.41792 13.41744 13.41592 13.41332 13.40960
## [201] 13.40470 13.39857 13.39118 13.38247 13.37240 13.36001 13.34459 13.32645
## [209] 13.30589 13.28321 13.25871 13.23270 13.20548 13.17735 13.14861 13.11958
## [217] 13.09055 13.06182 13.03369 13.00648 12.98048 12.95600 12.93334 12.91279
## [225] 12.89088 12.86435 12.83384 12.80000 12.76349 12.72496 12.68506 12.64443
## [233] 12.60374 12.56363 12.52475 12.48775 12.45329 12.42201 12.39457 12.37162
## [241] 12.35152 12.33221 12.31365 12.29580 12.27863 12.26211 12.24620 12.23087
## [249] 12.21608 12.20180 12.18800 12.17465 12.16170 12.14913 12.13690 12.12498
## [257] 12.11334 12.10193 12.09073 12.07971 12.06883 12.05805 12.04734 12.03667
## [265] 12.02601 12.01531 12.00456 11.99370 11.98272 11.97157 11.96022 11.94864
## [273] 11.93680 11.92531 11.91476 11.90501 11.89595 11.88744 11.87937 11.87161
## [281] 11.86403 11.85652 11.84895 11.84120 11.83314 11.82465 11.81560 11.80587
## [289] 11.79535 11.78454 11.77404 11.76381 11.75381 11.74402 11.73440 11.72491
## [297] 11.71552 11.70620 11.69692 11.68763 11.67831 11.66892 11.65942 11.64980
## [305] 11.64000 11.62999 11.61975 11.60924 11.59852 11.58771 11.57679 11.56578
## [313] 11.55467 11.54347 11.53218 11.52081 11.50935 11.49781 11.48618 11.47448
## [321] 11.46271 11.45086 11.43895 11.42696 11.41491 11.40280 11.39063 11.37840
## [329] 11.36612 11.35378 11.34140 11.32896 11.31648 11.30396 11.29140 11.27880
## [337] 11.26617 11.25350
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 338)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.63083 12.62602 12.62133 12.61675 12.61229 12.60793 12.60368 12.59953
##   [9] 12.59548 12.59152 12.58766 12.58388 12.58019 12.57657 12.57304 12.56957
##  [17] 12.56618 12.56285 12.55959 12.55639 12.55324 12.55015 12.54710 12.54410
##  [25] 12.54115 12.53823 12.53534 12.53249 12.52967 12.52688 12.52412 12.52141
##  [33] 12.51873 12.51611 12.51353 12.51100 12.50853 12.50612 12.50377 12.50149
##  [41] 12.49927 12.49713 12.49506 12.49307 12.49116 12.48933 12.48759 12.48594
##  [49] 12.48439 12.48293 12.48157 12.48032 12.47917 12.47813 12.47721 12.47640
##  [57] 12.47571 12.47514 12.47470 12.47438 12.47420 12.47416 12.47425 12.47448
##  [65] 12.47483 12.47528 12.47583 12.47648 12.47722 12.47807 12.47902 12.48007
##  [73] 12.48122 12.48247 12.48383 12.48528 12.48685 12.48852 12.49029 12.49217
##  [81] 12.49416 12.49625 12.49845 12.50076 12.50318 12.50570 12.50834 12.51109
##  [89] 12.51395 12.51692 12.52001 12.52320 12.52651 12.52994 12.53348 12.53713
##  [97] 12.54091 12.54479 12.54880 12.55293 12.55717 12.56154 12.56601 12.57059
## [105] 12.57527 12.58005 12.58491 12.58986 12.59489 12.60000 12.60517 12.61041
## [113] 12.61571 12.62106 12.62646 12.63191 12.63739 12.64291 12.64845 12.65402
## [121] 12.65960 12.66520 12.67080 12.67641 12.68201 12.68760 12.69318 12.69875
## [129] 12.70479 12.71177 12.71956 12.72808 12.73722 12.74688 12.75697 12.76737
## [137] 12.77799 12.78872 12.79948 12.81014 12.82063 12.83082 12.84063 12.84995
## [145] 12.85868 12.86671 12.87396 12.88276 12.89518 12.91072 12.92886 12.94910
## [153] 12.97092 12.99383 13.01731 13.04085 13.06395 13.08609 13.10677 13.12548
## [161] 13.14170 13.15494 13.16468 13.17260 13.18069 13.18892 13.19725 13.20565
## [169] 13.21409 13.22252 13.23093 13.23926 13.24750 13.25560 13.26353 13.27126
## [177] 13.27875 13.28597 13.29288 13.29946 13.30566 13.31145 13.31680 13.32168
## [185] 13.32605 13.32987 13.33311 13.33575 13.33773 13.33904 13.33964 13.33948
## [193] 13.33854 13.33679 13.33419 13.33070 13.32629 13.32094 13.31459 13.30723
## [201] 13.29881 13.28930 13.27867 13.26688 13.25390 13.23818 13.21849 13.19526
## [209] 13.16893 13.13993 13.10870 13.07568 13.04129 13.00598 12.97018 12.93432
## [217] 12.89885 12.86420 12.83080 12.79909 12.76950 12.74247 12.71844 12.69784
## [225] 12.67714 12.65286 12.62555 12.59572 12.56390 12.53063 12.49644 12.46185
## [233] 12.42739 12.39360 12.36100 12.33013 12.30150 12.27566 12.25313 12.23444
## [241] 12.21853 12.20393 12.19059 12.17842 12.16737 12.15735 12.14832 12.14019
## [249] 12.13290 12.12637 12.12055 12.11536 12.11073 12.10660 12.10290 12.09955
## [257] 12.09649 12.09365 12.09097 12.08837 12.08578 12.08315 12.08039 12.07744
## [265] 12.07423 12.07069 12.06676 12.06237 12.05744 12.05191 12.04571 12.03877
## [273] 12.03103 12.02338 12.01668 12.01080 12.00561 12.00096 11.99674 11.99280
## [281] 11.98902 11.98525 11.98137 11.97724 11.97273 11.96771 11.96204 11.95559
## [289] 11.94822 11.94052 11.93312 11.92599 11.91908 11.91235 11.90577 11.89929
## [297] 11.89286 11.88646 11.88003 11.87354 11.86694 11.86020 11.85327 11.84612
## [305] 11.83869 11.83096 11.82287 11.81440 11.80561 11.79663 11.78747 11.77812
## [313] 11.76859 11.75889 11.74901 11.73896 11.72875 11.71838 11.70785 11.69717
## [321] 11.68634 11.67537 11.66425 11.65300 11.64161 11.63009 11.61845 11.60669
## [329] 11.59481 11.58282 11.57071 11.55850 11.54619 11.53378 11.52128 11.50869
## [337] 11.49601 11.48324
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 338)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.02231 12.01741 12.01264 12.00799 12.00345 11.99901 11.99468 11.99045
##   [9] 11.98630 11.98223 11.97825 11.97433 11.97048 11.96668 11.96294 11.95924
##  [17] 11.95558 11.95196 11.94836 11.94478 11.94122 11.93767 11.93411 11.93056
##  [25] 11.92700 11.92341 11.91981 11.91618 11.91252 11.90881 11.90506 11.90125
##  [33] 11.89738 11.89345 11.88945 11.88537 11.88124 11.87707 11.87290 11.86870
##  [41] 11.86451 11.86031 11.85612 11.85194 11.84779 11.84366 11.83956 11.83550
##  [49] 11.83148 11.82752 11.82361 11.81977 11.81600 11.81231 11.80870 11.80517
##  [57] 11.80175 11.79842 11.79521 11.79211 11.78913 11.78628 11.78356 11.78098
##  [65] 11.77855 11.77628 11.77416 11.77221 11.77043 11.76883 11.76741 11.76603
##  [73] 11.76453 11.76293 11.76124 11.75946 11.75762 11.75571 11.75374 11.75174
##  [81] 11.74970 11.74765 11.74558 11.74351 11.74145 11.73941 11.73740 11.73543
##  [89] 11.73351 11.73165 11.72986 11.72816 11.72654 11.72503 11.72363 11.72235
##  [97] 11.72120 11.72020 11.71934 11.71866 11.71814 11.71781 11.71767 11.71774
## [105] 11.71802 11.71853 11.71927 11.72025 11.72150 11.72300 11.72479 11.72686
## [113] 11.72922 11.73189 11.73488 11.73820 11.74185 11.74586 11.75022 11.75494
## [121] 11.76005 11.76555 11.77144 11.77774 11.78447 11.79162 11.79921 11.80725
## [129] 11.81691 11.82915 11.84371 11.86032 11.87871 11.89860 11.91972 11.94182
## [137] 11.96461 11.98782 12.01119 12.03444 12.05731 12.07952 12.10080 12.12088
## [145] 12.13950 12.15638 12.17126 12.18740 12.20787 12.23207 12.25938 12.28920
## [153] 12.32092 12.35394 12.38766 12.42145 12.45473 12.48688 12.51730 12.54537
## [161] 12.57050 12.59208 12.60950 12.62480 12.64039 12.65623 12.67226 12.68844
## [169] 12.70472 12.72106 12.73741 12.75372 12.76994 12.78603 12.80194 12.81762
## [177] 12.83303 12.84811 12.86283 12.87713 12.89097 12.90429 12.91706 12.92923
## [185] 12.94074 12.95156 12.96163 12.97091 12.97934 12.98689 12.99351 12.99914
## [193] 13.00375 13.00728 13.00969 13.01092 13.01095 13.00970 13.00715 13.00323
## [201] 12.99792 12.99115 12.98287 12.97306 12.96165 12.94685 12.92726 12.90339
## [209] 12.87577 12.84491 12.81133 12.77555 12.73809 12.69947 12.66020 12.62082
## [217] 12.58183 12.54376 12.50712 12.47243 12.44022 12.41100 12.38530 12.36362
## [225] 12.34195 12.31630 12.28724 12.25536 12.22123 12.18542 12.14852 12.11109
## [233] 12.07373 12.03700 12.00148 11.96775 11.93639 11.90797 11.88307 11.86227
## [241] 11.84424 11.82724 11.81121 11.79611 11.78188 11.76847 11.75582 11.74388
## [249] 11.73260 11.72192 11.71180 11.70218 11.69300 11.68422 11.67578 11.66763
## [257] 11.65971 11.65198 11.64437 11.63685 11.62934 11.62181 11.61419 11.60645
## [265] 11.59851 11.59033 11.58186 11.57304 11.56382 11.55415 11.54398 11.53325
## [273] 11.52190 11.51070 11.50035 11.49072 11.48171 11.47320 11.46506 11.45718
## [281] 11.44945 11.44175 11.43395 11.42595 11.41762 11.40885 11.39952 11.38952
## [289] 11.37871 11.36766 11.35695 11.34654 11.33638 11.32643 11.31665 11.30699
## [297] 11.29740 11.28784 11.27827 11.26864 11.25891 11.24903 11.23896 11.22866
## [305] 11.21807 11.20716 11.19588 11.18418 11.17217 11.15997 11.14760 11.13504
## [313] 11.12231 11.10941 11.09634 11.08311 11.06971 11.05616 11.04245 11.02859
## [321] 11.01458 11.00042 10.98612 10.97169 10.95712 10.94241 10.92758 10.91262
## [329] 10.89754 10.88234 10.86702 10.85159 10.83606 10.82041 10.80466 10.78882
## [337] 10.77287 10.75684
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")